package com.example.zzl.adapter.SearchFragment;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.app.MyApplication;
import com.example.zzl.model.bean.SearchFragment.Cat;
import com.example.zzl.model.bean.SearchFragment.Mall;
import com.example.zzl.model.bean.SearchFragment.SearchBrand;
import com.example.zzl.utils.LruImageCache;
import com.example.zzl.view.activity.MainActivity;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/8.
 */

public class CifGvAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Object> list;
    private ArrayList<Cat.ListHotTagBean> cats;
    private ArrayList<Mall.ListHotTagBean> malls;
    private ArrayList<SearchBrand.ResultBeanX.ResultBean> results;
    private ImageLoader imageLoader;
    private int flag;

    public CifGvAdapter(Context context, ArrayList<Object> list) {
        this.context = context;
        this.list = list;
        cats = new ArrayList<>();
        malls = new ArrayList<>();
        results = new ArrayList<>();
        imageLoader = new ImageLoader(MyApplication.queue, LruImageCache.getInstance());


    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        Object o = list.get(position);
        if(o instanceof Cat.ListHotTagBean){
            cats.add((Cat.ListHotTagBean) list.get(position));
            return 0;
        }else if(o instanceof Mall.ListHotTagBean){
            malls.add((Mall.ListHotTagBean) list.get(position));
            //Log.e("data",malls.size()+"");
            flag = 1;
            return 1;
        }else if(o instanceof SearchBrand.ResultBeanX.ResultBean){
            results.add((SearchBrand.ResultBeanX.ResultBean) list.get(position));
            flag = 2;
            return 1;
        }
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder1 vh1;
        ViewHolder2 vh2;
        switch(getItemViewType(i)){
            case 0:
                if(view == null){
                    view = View.inflate(context, R.layout.cif_item_1,null);
                    vh1 = new ViewHolder1();
                    vh1.tv_1 = (TextView) view.findViewById(R.id.tv_cif_1);
                    vh1.tv_2 = (TextView) view.findViewById(R.id.tv_cif_2);
                    vh1.imageView = (ImageView)view.findViewById(R.id.iv_cif_1);
                    view.setTag(vh1);
                }else{
                    vh1 = (ViewHolder1) view.getTag();
                }
                vh1.tv_1.setText(cats.get(i).getStrTagName());
                vh1.tv_2.setText(cats.get(i).getStrTagDes());
                loadImgByVolley(cats.get(i).getStrTagImage(),vh1.imageView);
                break;
            case 1:
                if(view == null){
                    view = View.inflate(context, R.layout.cif_item_2,null);
                    vh2 = new ViewHolder2();
                    vh2.iv = (ImageView)view.findViewById(R.id.iv_cif_2);
                    view.setTag(vh2);
                }else{
                    vh2 = (ViewHolder2) view.getTag();
                }
                if(flag == 1){
                    loadImgByVolley(malls.get(i).getStrTagImage(),vh2.iv);
                }else if(flag == 2){
                    loadImgByVolley(results.get(i).getStrBrandLogo(),vh2.iv);
                }

                break;

        }
        return view;
    }

    private void loadImgByVolley(String string, ImageView ivImage) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivImage,
                R.drawable.ic_launcher, R.drawable.ic_launcher);
        imageLoader.get(string, listener);
    }

    class ViewHolder1{
        TextView tv_1,tv_2;
        ImageView imageView;
    }

    class ViewHolder2{
        ImageView iv;

    }


}
